package com.requiem.hist_group_manage_springboot.controller;

import com.requiem.hist_group_manage_springboot.common.TimeParserUtil;
import com.requiem.hist_group_manage_springboot.constants.SystemConstants;
import com.requiem.hist_group_manage_springboot.exception.R;
import com.requiem.hist_group_manage_springboot.mapper.ConfigsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/configs")
public class ConfigsController {

    @Autowired
    ConfigsMapper configsMapper;
    @Autowired
    TimeParserUtil timeParserUtil;

    @GetMapping("/getExpires")
    R getExpires() {
        Long expires = configsMapper.getExpires();
        Object s = timeParserUtil.formatMillisToTimeString(expires);
        return R.success(s);
    }

    @PostMapping({"/edit"})
    R edit(@RequestParam String expires) {
        long l = timeParserUtil.parseToMillis(expires);
        Long expires1 = configsMapper.getExpires();
        int i;
        if (expires1 == null) {
            i = configsMapper.addExpires(l);
        } else {
            i = configsMapper.editExpires(l);
        }
        if (i < 0) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }
}
